
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_sercry_manage/provider/provider.dart';

import '../../core/GlobalSecryManager.dart';
import '../../main.dart';
import 'home_drawer.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String? domain = null;
  String? password = null;
  String currentPassword = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: const HomeDrawer(),
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ListView(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SelectableText(currentPassword,
                          style:
                          const TextStyle(color: Colors.red, fontSize: 24)),
                      if (currentPassword.trim().isNotEmpty)
                        IconButton(
                            onPressed: () {
                              var clipData =
                              ClipboardData(text: currentPassword);
                              Clipboard.setData(clipData);
                            },
                            icon: const Icon(Icons.content_copy))
                    ],
                  ),
                ),
                Form(
                    key: _formKey,
                    child: Column(
                      children: <Widget>[
                        SizedBox.fromSize(
                          size: Size(500, 50),
                          child: TextFormField(
                            decoration: const InputDecoration(
                              hintText: '请输入Domain',
                            ),
                            validator: (String? value) {
                              if (value == null || value.isEmpty) {
                                return '请输入Domain';
                              }
                              return null;
                            },
                            onChanged: (value) => domain = value,
                          ),
                        ),
                        SizedBox.fromSize(
                          size: const Size(500, 50),
                          child: TextFormField(
                            obscureText: true,
                            decoration:
                            const InputDecoration(hintText: '请输入唯一密码'),
                            validator: (String? value) {
                              if (value == null || value.isEmpty) {
                                return '请输入唯一密码';
                              }
                              return null;
                            },
                            onChanged: (value) => password = value,
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 16.0),
                          child: ElevatedButton(
                            onPressed: () {
                              // Validate will return true if the form is valid, or false if
                              // the form is invalid.
                              if (_formKey.currentState!.validate()) {
                                // Process data.
                                GlobalSecryManager.generate(domain!, password!)
                                    .then((value) => {
                                  setState(() {
                                    currentPassword = value;
                                  }),
                                });
                              }
                            },
                            child: const Text('生成密码'),
                          ),
                        ),
                      ],
                    )),
                SizedBox(
                    width: 700,
                    height: 300,
                    child: Card(
                        elevation: 10,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: const <Widget>[
                            Icon(Icons.tips_and_updates),
                            Text(
                              """
 使用例子：
    
    domain = 'www.baidu.com'
    password = '123456'
    生成密钥：wudZWS385\$@@

    domain = 'www.bilibili.com'
    password = '123456'
    生成密钥：zztDAA282##\$
""",
                            )
                          ],
                        ))),
              ],
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          ThemeModeNotifier mode = context.watch<ThemeModeNotifier>();
          mode.mode = mode.mode == ThemeMode.light ? ThemeMode.dark : ThemeMode.light;
        },
        child: Icon(context.watch<ThemeModeNotifier>().mode == ThemeMode.dark
            ? Icons.light_mode
            : Icons.dark_mode),
      ),
    );

  }
}